package com.walkthecow.justdraw;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class PenSettingsAdvanced extends Activity implements AdapterView.OnItemSelectedListener{

	private Spinner spinnerEffect;
	private Spinner spinnerStyle;
	private Spinner spinnerCap;
	private Spinner spinnerJoin;
	private Spinner spinnerBlurStyle;

	private int mEffect;
	private int mStyle;
	private int mCap;
	private int mJoin;
	private int mBlurStyle;

	private SpinnerKeyValuePairs[] mEffectArray = new SpinnerKeyValuePairs[5];
	private SpinnerKeyValuePairs[] mStyleArray = new SpinnerKeyValuePairs[3];
	private SpinnerKeyValuePairs[] mCapArray = new SpinnerKeyValuePairs[3];
	private SpinnerKeyValuePairs[] mJoinArray = new SpinnerKeyValuePairs[3];
	private SpinnerKeyValuePairs[] mBlurStyleArray = new SpinnerKeyValuePairs[4];

	private EditText editTextShadowDX;
	private EditText editTextShadowDY;
	private EditText editTextShadowRadius;
	private EditText editTextBlurRadius;

	private EditText editTextEmbossDirection;
	private EditText editTextEmbossAmbient;
	private EditText editTextEmbossSpecular;
	private EditText editTextEmbossRadius;

	private float[] mEmbossDirection;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.pen_settings_advanced);

		editTextShadowDX=(EditText)findViewById(R.id.pen_adv_edittext_shadowdx);
		editTextShadowDY=(EditText)findViewById(R.id.pen_adv_edittext_shadowdy);
		editTextShadowRadius=(EditText)findViewById(R.id.pen_adv_edittext_shadowradius);
		editTextBlurRadius=(EditText)findViewById(R.id.pen_adv_edittext_blurradius);
		editTextEmbossDirection=(EditText)findViewById(R.id.pen_adv_edittext_embossdirection);
		editTextEmbossAmbient=(EditText)findViewById(R.id.pen_adv_edittext_embossambient);
		editTextEmbossRadius=(EditText)findViewById(R.id.pen_adv_edittext_embossradius);
		editTextEmbossSpecular=(EditText)findViewById(R.id.pen_adv_edittext_embossspecular);

		spinnerStyle = (Spinner)findViewById(R.id.pen_adv_spinner_style);
		spinnerJoin = (Spinner)findViewById(R.id.pen_adv_spinner_join);
		spinnerCap = (Spinner)findViewById(R.id.pen_adv_spinner_cap);
		spinnerBlurStyle = (Spinner)findViewById(R.id.pen_adv_spinner_blurstyle);
		spinnerEffect = (Spinner)findViewById(R.id.pen_adv_spinner_effect);

		setSpinnerKeyValuePairs();

		loadSpinner(spinnerEffect, mEffectArray);
		loadSpinner(spinnerBlurStyle, mBlurStyleArray);
		loadSpinner(spinnerCap, mCapArray);
		loadSpinner(spinnerJoin, mJoinArray);
		loadSpinner(spinnerStyle, mStyleArray);

		setInitialValues(getIntent().getExtras());

	}

	private void setInitialValues(Bundle extras){

		editTextShadowDX.setText(String.valueOf(extras.getFloat(Constants.KEY_SHADOWDX)));
		editTextShadowDY.setText(String.valueOf(extras.getFloat(Constants.KEY_SHADOWDY)));
		editTextShadowRadius.setText(String.valueOf(extras.getFloat(Constants.KEY_SHADOWRADIUS)));
		editTextBlurRadius.setText(String.valueOf(extras.getFloat(Constants.KEY_BLURRADIUS)));
		//editTextEmbossDirection.setText(String.valueOf(extras.getFloatArray(Constants.KEY_EMBOSSDIRECTION)));
		mEmbossDirection = extras.getFloatArray(Constants.KEY_EMBOSSDIRECTION);
		editTextEmbossAmbient.setText(String.valueOf(extras.getFloat(Constants.KEY_EMBOSSAMBIENT)));
		editTextEmbossRadius.setText(String.valueOf(extras.getFloat(Constants.KEY_EMBOSSRADIUS)));
		editTextEmbossSpecular.setText(String.valueOf(extras.getFloat(Constants.KEY_EMBOSSSPECULAR)));

		spinnerBlurStyle.setSelection(extras.getInt(Constants.KEY_BLURSTYLE));
		spinnerCap.setSelection(extras.getInt(Constants.KEY_CAP));
		spinnerEffect.setSelection(extras.getInt(Constants.KEY_EFFECT));
		spinnerJoin.setSelection(extras.getInt(Constants.KEY_JOIN));
		spinnerStyle.setSelection(extras.getInt(Constants.KEY_STYLE));
		
		
	}

	private void loadSpinner(Spinner s, final SpinnerKeyValuePairs[] items){
		ArrayAdapter<SpinnerKeyValuePairs> adapter = new ArrayAdapter<SpinnerKeyValuePairs>(this,android.R.layout.simple_spinner_item,items );
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);
		s.setOnItemSelectedListener(this);
	}

	private void setSpinnerKeyValuePairs(){
		mEffectArray[Constants.EFFECT_EMBOSS]=new SpinnerKeyValuePairs(getText(R.string.text_emboss).toString(), Constants.EFFECT_EMBOSS);
		mEffectArray[Constants.EFFECT_SHADOW]=new SpinnerKeyValuePairs(getText(R.string.text_shadow).toString(), Constants.EFFECT_SHADOW);
		mEffectArray[Constants.EFFECT_BLUR]=new SpinnerKeyValuePairs(getText(R.string.text_blur).toString(), Constants.EFFECT_BLUR);
		mEffectArray[Constants.EFFECT_NEON]=new SpinnerKeyValuePairs(getText(R.string.text_neon).toString(), Constants.EFFECT_NEON);
		mEffectArray[Constants.EFFECT_NEON]=new SpinnerKeyValuePairs(getText(R.string.text_normal).toString(), Constants.EFFECT_NONE);

		mStyleArray[Constants.STYLE_FILL]=new SpinnerKeyValuePairs(getText(R.string.Fill).toString(), Constants.STYLE_FILL);
		mStyleArray[Constants.STYLE_FILL_AND_STROKE]=new SpinnerKeyValuePairs(getText(R.string.FillandStroke).toString(), Constants.STYLE_FILL_AND_STROKE);
		mStyleArray[Constants.STYLE_STROKE]=new SpinnerKeyValuePairs(getText(R.string.Stroke).toString(), Constants.STYLE_STROKE);

		mCapArray[Constants.CAP_BUTT]=new SpinnerKeyValuePairs(getText(R.string.Butt).toString(), Constants.CAP_BUTT);
		mCapArray[Constants.CAP_ROUND]=new SpinnerKeyValuePairs(getText(R.string.Round).toString(), Constants.CAP_ROUND);
		mCapArray[Constants.CAP_SQUARE]=new SpinnerKeyValuePairs(getText(R.string.Square).toString(), Constants.CAP_SQUARE);

		mJoinArray[Constants.JOIN_BEVEL]=new SpinnerKeyValuePairs(getText(R.string.Bevel).toString(), Constants.JOIN_BEVEL);
		mJoinArray[Constants.JOIN_MITER]=new SpinnerKeyValuePairs(getText(R.string.Miter).toString(), Constants.JOIN_MITER);
		mJoinArray[Constants.JOIN_ROUND]=new SpinnerKeyValuePairs(getText(R.string.Round).toString(), Constants.JOIN_ROUND);

		mBlurStyleArray[Constants.BLUR_INNER]=new SpinnerKeyValuePairs(getText(R.string.INNER).toString(), Constants.BLUR_INNER);
		mBlurStyleArray[Constants.BLUR_NORMAL]=new SpinnerKeyValuePairs(getText(R.string.NORMAL).toString(), Constants.BLUR_NORMAL);
		mBlurStyleArray[Constants.BLUR_OUTER]=new SpinnerKeyValuePairs(getText(R.string.OUTER).toString(), Constants.BLUR_OUTER);
		mBlurStyleArray[Constants.BLUR_SOLID]=new SpinnerKeyValuePairs(getText(R.string.SOLID).toString(), Constants.BLUR_SOLID);
	}
	public void buttonClick(View view){

		switch(view.getId()){
		case R.id.pen_adv_button_cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		case R.id.pen_adv_button_ok:
			setResult(RESULT_OK, setPenIntent());
			finish();
			break;
		}		
	}

	private Intent setPenIntent(){
		Intent intent= new Intent();
		Bundle extras = new Bundle();

		//int effect = Integer.parseInt(spinnerEffect.getSelectedItem().toString().trim());
		//int blurStyle = Integer.parseInt(spinnerBlurStyle.getSelectedItem().toString().trim());
		//int cap = Integer.parseInt(spinnerCap.getSelectedItem().toString().trim());
		//int join = Integer.parseInt(spinnerJoin.getSelectedItem().toString().trim());
		//int style = Integer.parseInt(spinnerStyle.getSelectedItem().toString().trim());

		float shadowDX = Float.parseFloat(editTextShadowDX.getText().toString());
		float shadowDY = Float.parseFloat(editTextShadowDY.getText().toString().trim());
		float shadowRadius = Float.parseFloat(editTextShadowRadius.getText().toString().trim());
		float blurRadius = Float.parseFloat(editTextBlurRadius.getText().toString().trim());
		//float[] embossDirection = Float.parseFloat(editTextEmbossDirection.getText().toString().trim());
		float[] embossDirection = mEmbossDirection;
		float embossAmbient = Float.parseFloat(editTextEmbossAmbient.getText().toString().trim());
		float embossRadius = Float.parseFloat(editTextEmbossRadius.getText().toString().trim());
		float embossSpecular = Float.parseFloat(editTextEmbossSpecular.getText().toString().trim());	

		extras.putInt(Constants.KEY_EFFECT, mEffect);
		extras.putInt(Constants.KEY_BLURSTYLE, mBlurStyle);
		extras.putInt(Constants.KEY_CAP, mCap);
		extras.putInt(Constants.KEY_JOIN, mJoin);
		extras.putInt(Constants.KEY_STYLE, mStyle);

		extras.putFloat(Constants.KEY_SHADOWDX, shadowDX);
		extras.putFloat(Constants.KEY_SHADOWDY, shadowDY);
		extras.putFloat(Constants.KEY_SHADOWRADIUS, shadowRadius);
		extras.putFloat(Constants.KEY_BLURRADIUS, blurRadius);
		extras.putFloatArray(Constants.KEY_EMBOSSDIRECTION, embossDirection);
		extras.putFloat(Constants.KEY_EMBOSSAMBIENT, embossAmbient);
		extras.putFloat(Constants.KEY_EMBOSSRADIUS, embossRadius);
		extras.putFloat(Constants.KEY_EMBOSSSPECULAR, embossSpecular);

		intent.putExtras(extras);

		return intent;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		Spinner spinner = (Spinner) parent;
		SpinnerKeyValuePairs d;
		switch(spinner.getId()){
		case R.id.pen_adv_spinner_blurstyle:
			d = mBlurStyleArray[position];
			mBlurStyle = d.getValue();
			break;
		case R.id.pen_adv_spinner_cap:
			d = mBlurStyleArray[position];
			mBlurStyle = d.getValue();
			break;
		case R.id.pen_adv_spinner_effect:
			d = mEffectArray[position];
			mEffect = d.getValue();
			break;
		case R.id.pen_adv_spinner_join:
			d = mJoinArray[position];
			mJoin = d.getValue();
			break;
		case R.id.pen_adv_spinner_style:
			d = mStyleArray[position];
			mStyle = d.getValue();
			break;

		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub

	}

}
